package com.lions.client.view.gui;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.lions.client.controller.Controller;
import com.lions.client.model.Model;
import com.lions.client.view.basepanel.BasePanel;
import com.lions.client.view.buttonpanel.ButtonPanel;
import com.lions.client.view.mappanel.MapPanel;
import com.lions.client.view.statspanel.StatsPanel;
import com.lions.client.view.weather.WeatherPanel;
import com.lions.client.view.modulepanel.ModulePanel;

/**
 * This is the GUI for the main panel.  This panel holds all of the
 * panels used for functionality.  This panel also takes/ gives
 * the model and/or controller to every panel.
 * @author Brad
 * 
 */
public class MainPanel extends ScrollPanel {

	

//	/**
//	 * Created after successful login.
//	 */
//	public MainPanel(Model m, Controller c) {
//		super();
//		model = m;
//		control = c;
//		initComponents();
//
//	}
	
//	public MainPanel() {
//		super();
//		initComponents();
//	}
	
	public MainPanel(Model m) {
		super();
		model = m;
		initComponents();

	}

	/**
	 * Builds the GUI components
	 * There is:
	 * menuBar (Top)
	 * buttonPanel (North) 
	 * modulePanel (West)
	 * splitLayout (Center)
	 * 
	 */
	private void initComponents() {
		// Set CSS for this panel
		//this.setStyleName("scrollpanel");



		// Stack Layout Panel for map and stats
		mapPanel = new MapPanel(model);
		mapPanel.addStyleName("mappanel");
		centerStackLayout = new StackLayoutPanel(Unit.EM);
		centerStackLayout.setStyleName("splitlayoutstyle");
		centerStackLayout.add(mapPanel, new HTML("Map"), tabSize);
		centerStackLayout.add(new StatsPanel(model), new HTML("Module Collection Statistics"), tabSize);
		centerPanel = new LayoutPanel();
		centerPanel.add(centerStackLayout);
		centerPanel.addStyleName("centerpanel");
		
		// Base side panel
		basePanel = new BasePanel(model, mapPanel);
		eastPanel = new LayoutPanel();
		eastPanel.add(basePanel);
		eastPanel.setStyleName("sidepanel");

		// Module side panel
		modulePanel = new ModulePanel(model, mapPanel, basePanel);
		westPanel = new LayoutPanel();
		westPanel.add(modulePanel);
		westPanel.addStyleName("sidepanel");
		
		// Controller panel
		buttonPanel = new ButtonPanel(model, mapPanel, modulePanel);
		northPanel = new DockPanel();
		northPanel.add(buttonPanel, DockPanel.CENTER);
		northPanel.addStyleName("northpanel");
		
		// Additional Content Panel
		southPanel = new DockPanel();
		southPanel.add(new WeatherPanel(), DockPanel.CENTER);
		southPanel.addStyleName("southpanel");

		// Add panels to widgetPanel
		widgetPanel = new DockPanel();
		widgetPanel.setSpacing(4);
		widgetPanel.add(northPanel, DockPanel.NORTH);
		widgetPanel.add(southPanel, DockPanel.SOUTH);
		widgetPanel.add(westPanel, DockPanel.WEST);
		widgetPanel.add(eastPanel, DockPanel.EAST);
		widgetPanel.add(centerPanel, DockPanel.CENTER);
		
		// Menu Bar
		menu = new MyMenuBar(model, modulePanel);
		menu.addStyleName("menubar");
		
		// Add the panels to this panel
		mainDock = new DockPanel();
		mainDock.addStyleName("dockpanel");
		mainDock.setHorizontalAlignment(DockPanel.ALIGN_CENTER);
		mainDock.add(menu, DockPanel.NORTH);
		mainDock.add(widgetPanel, DockPanel.CENTER);
		
		//loadSessionStorage();
		
		this.add(mainDock);
	}
	
	public MapPanel getMapPanel() {
		return mapPanel;
	}
	
	public ModulePanel getModulePanel() {
		return modulePanel;
	}
	
	public BasePanel getBasePanel() {
		return basePanel;
	}
	
	public ButtonPanel getButtonPanel() {
		return buttonPanel;
	}
	
//	/**
//	 * Loads the data from session storage (if there is any) This method
//	 * currently loads the modules
//	 * 
//	 * @param map
//	 *            MapPanel to update
//	 */
//	private void loadSessionStorage() {
//
//		model.getHTMLStorage().storageLoad();
//		// Populates the module list from any storage.
//		modulePanel.populate();
//		// Updates the mapPanel after storage is restored.
//		mapPanel.populate();
//	}

	private int tabSize = 2;
	private MyMenuBar menu;
	private DockPanel mainDock;
	private DockPanel widgetPanel;
	private DockPanel northPanel;
	private DockPanel southPanel;
	private LayoutPanel westPanel;
	private LayoutPanel eastPanel;
	private LayoutPanel centerPanel;
	private StackLayoutPanel centerStackLayout;
	private ModulePanel modulePanel;
	private BasePanel basePanel;
	private MapPanel mapPanel;
	private ButtonPanel buttonPanel;
	private Model model;
	//private Controller control;

}
